<?php namespace Home\Controller;

use Framework\Kernel\Controller;

use Home\Model\Category;

use Home\Model\Ask;

use Framework\Model\Db;
class AskController extends Controller
{
	public function add()
	{
		if(IS_POST)
		{
			$ask = new Ask;
			if($id = $ask->add())
			{
				$this->success('发表成功',U('ask',['id'=>$id]));
			}
			else
			{
				$this->error($ask->getError());
			}
		}
		else
		{
			$this->assign('category',(new Category)->getAll());
			$this->display('add.php');
		}
	}

	public function ask()
	{
		$id = $_GET['id'];
		$ask = new Ask;
		$field = $ask->where("id=$id")->first();
		$this->assign('field',$field);
		$this->display('ask.php');
	}

	public function getClick()
	{
		$id= $_GET['id'];

		//更新点击数
		$sql = "UPDATE ask set click=click+1 WHERE id=$id";
		(new Db('ask'))->execute($sql);

		$ask = (new Db('ask'))->where("id=$id")->first();
		echo "document.write({$ask['click']})";
		exit;

	}
}
















